//
//  LoginViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "HomeViewController.h"
#import "WeiboSDK.h"
#import "UMSocialDataService.h"

@interface LoginViewController ()<UMSocialDataDelegate>

@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic, strong) NSArray *permissions; // 权限

@end

@implementation LoginViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 权限
        self.permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t", nil];
    }
    return self;
}

- (void)loadView
{
    self.loginView = [[LoginView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.view = self.loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadNavigationIndfo];
    // QQ登陆按钮触发方法
    [self.loginView.qqButton addTarget:self action:@selector(QQButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 微博登陆触发方法
    [self.loginView.sinaButton addTarget:self action:@selector(sinaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 监听新浪微博登录成功的广播
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sinaLoginSuccessed) name:kSinaLoginSuccessed object:nil];
}

#pragma mark - 新浪微博登录成功后的处罚方法
- (void)sinaLoginSuccessed
{
    [self backAction];
}

#pragma mark - QQ登陆触发方法
- (void)QQButtonAction:(UIButton *)button
{
    [[TheThirdCall theThirdCallShare].tencentOauth authorize:self.permissions inSafari:NO];
    
    // 返回上个页面
    [self backAction];
}

#pragma mark - 微博登陆
- (void)sinaButtonAction:(UIButton *)button
{
//    [[TheThirdCall theThirdCallShare] sinaSSO:self];
    
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    
    [WeiboSDK sendRequest:request];
    
//        [[TheThirdCall theThirdCallShare] getSinaUserInfo2];
//    [self backAction];
    
}


#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    self.navigationItem.title = @"登陆";
    // 取消
    UIBarButtonItem *leftBackButton = [UIBarButtonItem itemWithTarget:self Action:@selector(backAction) image:@"home_back" highImage:@"home_back_highlighted"];
    self.navigationItem.leftBarButtonItem = leftBackButton;
    
    // 标题
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

#pragma mark - 返回按钮触发事件
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}


@end
